//
//  FillInTheOrderViewController.m
//  EastMarket
//
//  Created by 李鹏程 on 16/11/21.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "FillInTheOrderViewController.h"

static NSString *HttpBaseURL = @"http://m.donglin.me/api";

@interface FillInTheOrderViewController ()
{
    NSDictionary *createOrderDic;
}
@end

@implementation FillInTheOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"填写订单";
    self.view.backgroundColor = EastMarket_backgroundColor;
    _dataSourceArr = [[NSMutableArray alloc] init];
    _deliveryDayListArr = [[NSMutableArray alloc] init];
    _payTypeListArr = [[NSMutableArray alloc] init];
    
    _itemInfoListArr = [[NSMutableArray alloc] init];
    _orderInfoListArr = [[NSMutableArray alloc] init];
    [self initFillInOrderUI];
    [_dataSourceArr addObjectsFromArray:[_checkResultDic objectForKey:@"sellerOrderList"]];

    NSMutableDictionary *itemDict = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *orderInfoDict = [[NSMutableDictionary alloc] init];
    for (NSDictionary *dict in [_checkResultDic objectForKey:@"sellerOrderList"]) {
        for (NSDictionary *dic in [dict objectForKey:@"itemList"]) {
            
            [itemDict setObject:[dic objectForKey:@"itemId"] forKey:@"itemId"];
            [itemDict setObject:[dic objectForKey:@"num"] forKey:@"num"];
            [_itemInfoListArr addObject:itemDict];
        }
        [orderInfoDict setObject:[dict objectForKey:@"sellerId"] forKey:@"sellerId"];
        if ([[[dict objectForKey:@"payTypeList"] objectAtIndex:0] objectForKey:@"value"]) {
            [orderInfoDict setObject:[[[dict objectForKey:@"payTypeList"] objectAtIndex:0] objectForKey:@"value"] forKey:@"payType"];
        }
        if ([[[dict objectForKey:@"deliveryDayList"] objectAtIndex:0] objectForKey:@"fullDate"]) {
            [orderInfoDict setObject:[[[dict objectForKey:@"deliveryDayList"] objectAtIndex:0] objectForKey:@"fullDate"] forKey:@"expectedDeliveryTime"];
        }
        [_orderInfoListArr addObject:orderInfoDict];
    }
    [_tabView reloadData];
    debugLog(@"_itemInfoListArr:%@", _itemInfoListArr);
}

- (void)initFillInOrderUI {

    CGFloat spacing = 8.0f;
    CGFloat viewHeight = 44.0f;

    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TopBarHeight - 1 - TabBarHeight) style:UITableViewStyleGrouped];
    _tabView.showsVerticalScrollIndicator = false;
    _tabView.delegate = self;
    _tabView.dataSource = self;
    //    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabView.backgroundColor = EastMarket_backgroundColor;
    [self setExtraCellLineHidden:_tabView];
    [self.view addSubview:_tabView];
   
    /*
    UIView *viewAddress = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, viewHeight * 2)];
    viewAddress.backgroundColor = EastMatket_AddressColor;
    _tabView.tableHeaderView = viewAddress;
    
    UILabel *lblReceiverNameAndReceiverPhone = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, viewAddress.frame.size.width - 2 * spacing, viewHeight)];
    lblReceiverNameAndReceiverPhone.text = [NSString stringWithFormat:@"%@ %@", [_checkResultDic objectForKey:@"receiverName"], [_checkResultDic objectForKey:@"receiverPhone"]];
    [viewAddress addSubview:lblReceiverNameAndReceiverPhone];
    
    UILabel *lblReceiverAddress = [[UILabel alloc] initWithFrame:CGRectMake(spacing, lblReceiverNameAndReceiverPhone.frame.origin.y + lblReceiverNameAndReceiverPhone.frame.size.height, viewAddress.frame.size.width - 2 * spacing, viewHeight)];
    lblReceiverAddress.text = [NSString stringWithFormat:@"%@", [_checkResultDic objectForKey:@"receiverAddress"]];
    [viewAddress addSubview:lblReceiverAddress];
    */
    UIView *viewAddress = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, viewHeight * 2)];
    viewAddress.backgroundColor = EastMatket_AddressColor;
    
    CALayer *bottomBorder = [CALayer layer];
    float height = viewAddress.frame.size.height - 2.0f;
    float width=viewAddress.frame.size.width;
    bottomBorder.frame = CGRectMake(0.0f, height, width, 2.0f);
    bottomBorder.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"rainbow_line"]].CGColor;
    [viewAddress.layer addSublayer:bottomBorder];
    
    _tabView.tableHeaderView = viewAddress;
    
    UILabel *lblReceiverNameAndReceiverPhone = [[UILabel alloc] initWithFrame:CGRectMake(spacing * 2 + 22, spacing, viewAddress.frame.size.width - 2 * spacing, viewHeight - spacing)];
    lblReceiverNameAndReceiverPhone.text = [NSString stringWithFormat:@"%@   %@", [_checkResultDic objectForKey:@"receiverName"], [_checkResultDic objectForKey:@"receiverPhone"]];
    lblReceiverNameAndReceiverPhone.font = [UIFont systemFontOfSize:16];
    [viewAddress addSubview:lblReceiverNameAndReceiverPhone];
    
    UIImageView *markImg = [[UIImageView alloc] initWithFrame:CGRectMake(spacing, (viewHeight - 22 - lblReceiverNameAndReceiverPhone.frame.origin.y) / 2 + lblReceiverNameAndReceiverPhone.frame.origin.y + lblReceiverNameAndReceiverPhone.frame.size.height, 22, 22)];
    markImg.image = [UIImage imageNamed:@"地理位置"];
    [viewAddress addSubview:markImg];
    
    UILabel *lblReceiverAddress = [[UILabel alloc] initWithFrame:CGRectMake(markImg.frame.size.width + markImg.frame.origin.x + spacing, lblReceiverNameAndReceiverPhone.frame.origin.y + lblReceiverNameAndReceiverPhone.frame.size.height, viewAddress.frame.size.width - 2 * spacing - markImg.frame.origin.x - markImg.frame.size.width, viewHeight - spacing)];
    lblReceiverAddress.text = [NSString stringWithFormat:@"%@",  [_checkResultDic objectForKey:@"receiverAddress"]];
    lblReceiverAddress.font = [UIFont systemFontOfSize:16];
    lblReceiverAddress.numberOfLines = 0;
    [viewAddress addSubview:lblReceiverAddress];
    
    UIView *viewTotalPrice = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - TopBarHeight - TabBarHeight, SCREEN_WIDTH * 3/ 4, TabBarHeight)];
    viewTotalPrice.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewTotalPrice];
    _lblTotalPrice = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, viewTotalPrice.frame.size.width - 2 * spacing, viewTotalPrice.frame.size.height)];
    _lblTotalPrice.backgroundColor = [UIColor clearColor];
    _lblTotalPrice.textAlignment = NSTextAlignmentLeft;
    _lblTotalPrice.text = [NSString stringWithFormat:@"合计: %.2lf", [[_checkResultDic objectForKey:@"totalAmount"] floatValue]];
    [viewTotalPrice addSubview:_lblTotalPrice];
    
    UIView *viewBottom = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 / 4, SCREEN_HEIGHT - TopBarHeight - TabBarHeight, SCREEN_WIDTH / 4, TabBarHeight)];
    viewBottom.backgroundColor = EastMarket_MainToneColor;
    [self.view addSubview:viewBottom];
    
    UIButton *btnBuy = [UIButton buttonWithType:UIButtonTypeSystem];
    btnBuy.frame = CGRectMake(spacing * 2, spacing, viewBottom.frame.size.width - spacing * 4, viewBottom.frame.size.height - 2 * spacing);
    btnBuy.backgroundColor = [UIColor clearColor];
    [btnBuy setTitle:@"提交订单" forState:UIControlStateNormal];
    [btnBuy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnBuy.titleLabel setFont:[UIFont systemFontOfSize:14]];
    btnBuy.layer.cornerRadius = 22.0f;
    [btnBuy addTarget:self action:@selector(btnBuyClicked:) forControlEvents:UIControlEventTouchUpInside];
    [viewBottom addSubview:btnBuy];
}

- (void)btnBuyClicked:(UIButton *)btn {

    [SVProgressHUD showWithStatus:@"加载中"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:EastMarket_MainToneColor];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];

    NSMutableDictionary *commitDict = [[NSMutableDictionary alloc] init];
    [commitDict setObject:_itemInfoListArr forKey:@"itemInfoList"];
    [commitDict setObject:_orderInfoListArr forKey:@"orderInfoList"];
    debugLog(@"commitDict:%@", commitDict);
//    [self loadViewAddOrder:commitDict];
    [self loadViewCreateOrder:commitDict];
}

- (void)loadViewCreateOrder:(NSDictionary *)dict {

    createOrderDic = dict;
    [NSThread detachNewThreadSelector:@selector(callServerCreateOrder) toTarget:self withObject:nil];
}

- (void)callServerCreateOrder {

    NSDictionary *resultOfCreateOrder = [AppServiceInterface OrderCreate:[self dictionaryToJson:createOrderDic]];
    [self performSelectorOnMainThread:@selector(updateUIAfterAddOrder:) withObject:resultOfCreateOrder waitUntilDone:NO];
}

- (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


#pragma mark -- 提交订单
// 下单
- (void)loadViewAddOrder:(NSDictionary *)dict {
    
    [NSThread detachNewThreadSelector:@selector(callServerAddOrder) toTarget:self withObject:Nil];
    _addOrderDic = dict;
}

- (void)callServerAddOrder {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic setObject:_addOrderDic forKey:@"data"];
    
    NSData *jsonData1 = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *str =  [[NSString alloc] initWithData:jsonData1 encoding:NSUTF8StringEncoding];
    
    NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:str, @"data",nil];
    
    if ([NSJSONSerialization isValidJSONObject:userDictionary])
    {
        NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData1];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/order/create", HttpBaseURL]];
        debugLog(@"********************************************url:%@\nparams:%@", url, dic);
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
        [request addRequestHeader:@"Accept" value:@"application/json"];
        [request setRequestMethod:@"POST"];
        [request setPostBody:tempJsonData];
        [request startSynchronous];
        NSError *error1 = [request error];
        debugLog(@"error:%@", error1);
        if (!error1)
        {
            NSString *response = [request responseString];
            //            debugLog(@"response：%@",response);
            NSData* jsonData = [response dataUsingEncoding:NSUTF8StringEncoding];
            //            debugLog(@"jsonData：%@",jsonData);
            NSString *strTest =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            debugLog(@"%@", strTest);
            resultsDictionary = [NSJSONSerialization JSONObjectWithData:[response dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
        }
    }
    debugLog(@"resultsDictionary:%@", resultsDictionary);
    [self performSelectorOnMainThread:@selector(updateUIAfterAddOrder:) withObject:resultsDictionary waitUntilDone:NO];
}

- (void)updateUIAfterAddOrder:(NSDictionary *)dic {
    
    [SVProgressHUD dismiss];
    if (dic && [[dic objectForKey:@"success"] integerValue] == 1) {
        
        debugLog(@"%@", _checkResultDic);
//        [GlobalUnit showAlertHint:@"㊗️恭喜您下单成功！" message:[NSString stringWithFormat:@"订单ID为：%@", [[[dic objectForKey:@"data"] objectForKey:@"orderIdList"] objectAtIndex:0]]];
        CheckoutCounterViewController *checkoutVC = [[CheckoutCounterViewController alloc] init];
        checkoutVC.strItemId = [NSString stringWithFormat:@"%@", [[[dic objectForKey:@"data"] objectForKey:@"orderIdList"] objectAtIndex:0]];
        checkoutVC.strCurAmount = [NSString stringWithFormat:@"%@", [_checkResultDic objectForKey:@"totalAmount"]];
        checkoutVC.orderNum = [NSString stringWithFormat:@"%ld", [[_checkResultDic objectForKey:@"totalNum"] integerValue]];
        
        [self.navigationController pushViewController:checkoutVC animated:true];
//        PayViewController *payVC = [[PayViewController alloc] init];
//        [self.navigationController pushViewController:payVC animated:true];
        
    } else if (dic && [[dic objectForKey:@"success"] integerValue] == 0) {
        [GlobalUnit showAlertHint:@"温馨提示！" message:[dic objectForKey:@"message"]];
        [SVProgressHUD dismiss];
    }
    else {
        
        [SVProgressHUD showErrorWithStatus:@"请求服务器失败，稍后请重试"];
        [SVProgressHUD dismiss];
    }
}

- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

#pragma mark -- UITableViewDelegate &  UITableViewDatasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _dataSourceArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *itemListDic = _dataSourceArr[section];
    NSArray * itemListDicArr = itemListDic[@"itemList"];
    return itemListDicArr.count;
}

- (void)updateSettlementValue:(NSString *)price {
    
//    lblSettlementValue.text = [NSString stringWithFormat:@"合计:  %.2lf", price.floatValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cell";
    OrderDetailViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[OrderDetailViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    NSDictionary *sellerNameDict = _dataSourceArr[indexPath.section];
    NSArray *itemListArr = sellerNameDict[@"itemList"];
    [cell.imgProductIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"pic"]]] placeholderImage:[UIImage imageNamed:@"NoProductImage"]];
    cell.lblProductName.text = [itemListArr[indexPath.row] objectForKey:@"title"];
    cell.lblProductPrice.text = [NSString stringWithFormat:@"%.2lf元", [[itemListArr[indexPath.row] objectForKey:@"totalAmount"] floatValue]];
    cell.lblProductNum.text = [NSString stringWithFormat:@"X %ld", [[itemListArr[indexPath.row] objectForKey:@"num"] integerValue]];
    return cell;
}

/*
 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 
 NSDictionary *sellerNameDict = _cartItemGroupListArr[section];
 return sellerNameDict[@"sellerName"];
 }
 */

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 56)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *viewSpacing = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, 16)];
    [view addSubview:viewSpacing];
    viewSpacing.backgroundColor = EastMarket_backgroundColor;
    
    UILabel *lblTitleHeader = [[UILabel alloc]initWithFrame:CGRectMake(8, viewSpacing.frame.origin.y + viewSpacing.frame.size.height, (view.frame.size.width - 16 - 1) * 5 / 6, 40)];
    lblTitleHeader.font = [UIFont systemFontOfSize:16];
    [view addSubview:lblTitleHeader];
    
    UILabel *lblArriveDate = [[UILabel alloc] initWithFrame:CGRectMake(lblTitleHeader.frame.origin.x + lblTitleHeader.frame.size.width, lblTitleHeader.frame.origin.y + 8, (view.frame.size.width - 16 - 1) / 6, 24)];
    lblArriveDate.backgroundColor = EastMarket_MainToneColor;
    lblArriveDate.text = @"次日达";
    lblArriveDate.textColor = [UIColor whiteColor];
    lblArriveDate.textAlignment = NSTextAlignmentCenter;
    lblArriveDate.font = [UIFont systemFontOfSize:14];
    [view addSubview:lblArriveDate];
    
    NSDictionary *sellerNameDict = _dataSourceArr[section];
    lblTitleHeader.text = sellerNameDict[@"sellerName"];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    NSDictionary *sellerNameDict = _dataSourceArr[section];
    
    
    CGFloat viewHeight = 40.0f;
    CGFloat spacing = 8.0f;
    CGFloat border = 1.0f;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 4 * viewHeight + 3 * border)];
    view.backgroundColor = [UIColor clearColor];
    
    UIView *viewPayType = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, viewHeight)];
    viewPayType.backgroundColor = [UIColor whiteColor];
    [view addSubview:viewPayType];
    if (!strPayTypeGloabal) {
        strPayTypeGloabal = [NSString stringWithFormat:@"%@", [[sellerNameDict[@"payTypeList"] objectAtIndex:0] objectForKey:@"title"]];
    }
    [self addSubDetailViews:viewPayType iconName:@"" iconLeft:8 title:@"付款方式" titleLeft:spacing hint:strPayTypeGloabal];
    //    UITapGestureRecognizer *tapViewPayType = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewPayType:)];
//    [viewPayType addGestureRecognizer:tapViewPayType];
    UIButton *btnPayType = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, viewPayType.frame.size.width, viewPayType.frame.size.height)];
    btnPayType.backgroundColor = [UIColor clearColor];
    btnPayType.tag = section + 1000;
    debugLog(@"****************************************************************** btnPayType.tag:%ld", btnPayType.tag);
    [btnPayType addTarget:self action:@selector(PayType:) forControlEvents:UIControlEventTouchUpInside];
    [viewPayType addSubview:btnPayType];
    
    UIView *viewDeliveryAmount = [[UIView alloc] initWithFrame:CGRectMake(0, viewPayType.frame.origin.y + viewPayType.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewDeliveryAmount.backgroundColor = [UIColor whiteColor];
    [view addSubview:viewDeliveryAmount];
    [self addSubDetailViews:viewDeliveryAmount iconName:@"" iconLeft:8 title:@"送货金额" titleLeft:spacing hint:[NSString stringWithFormat:@"￥%.2lf", [sellerNameDict[@"freightFee"] floatValue]]];
    
    UIView *viewDeliveryDate = [[UIView alloc] initWithFrame:CGRectMake(0, viewDeliveryAmount.frame.origin.y + viewDeliveryAmount.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewDeliveryDate.backgroundColor = [UIColor whiteColor];
    [view addSubview:viewDeliveryDate];
    if (!strDeliveryDayGlobal) {
        strDeliveryDayGlobal = [NSString stringWithFormat:@"%@", [[sellerNameDict[@"deliveryDayList"] objectAtIndex:0] objectForKey:@"shortDate"]];
    }
    [self addSubDetailViews:viewDeliveryDate iconName:@"" iconLeft:8 title:@"送达时间" titleLeft:spacing hint:strDeliveryDayGlobal];
    UITapGestureRecognizer *tapViewDeliveryDate = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewDeliveryDate:)];
    [viewDeliveryDate addGestureRecognizer:tapViewDeliveryDate];
    
    UIButton *btnDeliveryDate = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, viewPayType.frame.size.width, viewPayType.frame.size.height)];
    btnDeliveryDate.backgroundColor = [UIColor clearColor];
    btnDeliveryDate.tag = section + 1000;
    debugLog(@"****************************************************************** btnPayType.tag:%ld", btnDeliveryDate.tag);
    [btnDeliveryDate addTarget:self action:@selector(DeliveryDate:) forControlEvents:UIControlEventTouchUpInside];
    [viewDeliveryDate addSubview:btnDeliveryDate];

    
    UIView *viewTotal = [[UIView alloc] initWithFrame:CGRectMake(0, viewDeliveryDate.frame.origin.y + viewDeliveryDate.frame.size.height + border, SCREEN_WIDTH, viewHeight)];
    viewTotal.backgroundColor = [UIColor whiteColor];
    [view addSubview:viewTotal];
    
    UILabel *lblTotalNum = [[UILabel alloc] initWithFrame:CGRectMake(spacing, 0, (viewTotal.frame.size.width - 2 * spacing - border * 2) / 2, viewTotal.frame.size.height)];
    lblTotalNum.textColor = EastMarket_darkGreyFontColor;
    lblTotalNum.text = [NSString stringWithFormat:@"共%ld件商品", [sellerNameDict[@"totalNum"] integerValue]];
    [viewTotal addSubview:lblTotalNum];
    
    UILabel *lblTotalHead = [[UILabel alloc] initWithFrame:CGRectMake(lblTotalNum.frame.origin.x + lblTotalNum.frame.size.width + border, lblTotalNum.frame.origin.y, (viewTotal.frame.size.width - 2 * spacing - border * 2) / 4, lblTotalNum.frame.size.height)];
    lblTotalHead.text = @"合计:";
    lblTotalHead.textAlignment = NSTextAlignmentRight;
    lblTotalHead.font = [UIFont systemFontOfSize:14];
    lblTotalHead.textColor = EastMarket_darkGreyFontColor;
    [viewTotal addSubview:lblTotalHead];
    
    UILabel *lblTotalAmount = [[UILabel alloc] initWithFrame:CGRectMake(lblTotalHead.frame.origin.x + lblTotalHead.frame.size.width + border, lblTotalNum.frame.origin.y, (viewTotal.frame.size.width - 2 * spacing - border * 2)  / 4, lblTotalNum.frame.size.height)];
    lblTotalAmount.textColor = EastMarket_MainToneColor;
    lblTotalAmount.textAlignment = NSTextAlignmentRight;
    lblTotalAmount.text = [NSString stringWithFormat:@"￥%.2lf", [sellerNameDict[@"totalAmount"] floatValue]];
    [viewTotal addSubview:lblTotalAmount];
    
    /*
    NSMutableDictionary *orderInfoDict = [[NSMutableDictionary alloc] init];
    [orderInfoDict setObject:[sellerNameDict objectForKey:@"sellerId"] forKey:@"sellerId"];
    [orderInfoDict setObject:[[[sellerNameDict objectForKey:@"payTypeList"] objectAtIndex:0] objectForKey:@"value"] forKey:@"payType"];
    [orderInfoDict setObject:[[[sellerNameDict objectForKey:@"deliveryDayList"] objectAtIndex:0] objectForKey:@"shortDate"] forKey:@"expectedDeliveryTime"];
    [_orderInfoListArr addObject:orderInfoDict];
    */
    UIView *viewSpacing = [[UIView alloc] initWithFrame:CGRectMake(0, viewTotal.frame.origin.y + viewTotal.frame.size.height, view.frame.size.width, 8)];
    viewSpacing.backgroundColor = [UIColor clearColor];
//    [view addSubview:viewSpacing];
    
//    NSDictionary *sellerNameDict = _dataSourceArr[section];
//    lblTitleHeader.text = sellerNameDict[@"sellerName"];
//    [view addSubview:lblTitleHeader];
    return view;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 56;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 160 + 3;
}

- (void)tapViewPayType:(UIGestureRecognizer *)tap {
    
    debugMethod();
}

- (void)PayType:(UIButton *)sender {
   
    debugLog(@"%ld", sender.tag - 1000);
    NSDictionary *sellerNameDict = _dataSourceArr[sender.tag - 1000];
    [_payTypeListArr removeAllObjects];
    [_payTypeListArr addObjectsFromArray:[sellerNameDict objectForKey:@"payTypeList"]];
    debugLog(@"%@", _payTypeListArr);
    [self showPickerView:false selectList:_payTypeListArr indexPath:sender.tag - 1000];
}

- (void)DeliveryDate:(UIButton *)sender {

    debugLog(@"%ld", sender.tag - 1000);
    NSDictionary *sellerNameDict = _dataSourceArr[sender.tag - 1000];
    [_deliveryDayListArr removeAllObjects];
    [_deliveryDayListArr addObjectsFromArray:[sellerNameDict objectForKey:@"deliveryDayList"]];
    debugLog(@"%@", _deliveryDayListArr);
    [self showPickerView:true selectList:_deliveryDayListArr indexPath:sender.tag - 1000];
}
- (void)tapViewDeliveryDate:(UIGestureRecognizer *)tap {
    
    debugMethod();
}

//各条件view块添加详细内容
-(UILabel *)addSubDetailViews:(UIView *)parentView iconName:(NSString *)imageName iconLeft:(CGFloat)iconX title:(NSString*)titleName titleLeft:(CGFloat)titleX hint:(NSString*)hintText{
    //开头图标
    UIImageView *viewIcon = [[UIImageView alloc] initWithFrame:CGRectMake(iconX, 0, 20, 20)];
    [parentView addSubview:viewIcon];
    viewIcon.center = CGPointMake(viewIcon.center.x, parentView.frame.size.height/2);
    [viewIcon setImage:[UIImage imageNamed:imageName]];
    
    //标题
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(titleX, 0, 64 + 20, 32)];
    [parentView addSubview:lblTitle];
    lblTitle.center = CGPointMake(lblTitle.center.x, parentView.frame.size.height/2);
    lblTitle.numberOfLines = 1;
    lblTitle.text = titleName;
    lblTitle.lineBreakMode = NSLineBreakByWordWrapping;
    //lblTitle.textColor = [UIColor colorWithRed:167.0/255 green:167.0/255 blue:167.0/255 alpha:1];
    [lblTitle setTextColor:EastMarket_darkGreyFontColor];
    lblTitle.backgroundColor =[UIColor clearColor];
    lblTitle.font = [UIFont fontWithName:@"Arial" size:16];
    
    //右箭头  15*15，距离右边也是15
    UIImageView *viewArrow = [[UIImageView alloc] initWithFrame:CGRectMake(parentView.frame.size.width-15-15, 0, 15, 15)];
    
    viewArrow.center = CGPointMake(viewArrow.center.x, parentView.frame.size.height/2);
    [viewArrow setImage:[UIImage imageNamed:@"rightArrow"]];
    
    //内容，初始化时为提示文字
    CGFloat left = 62;
    if ([GlobalUnit isBlankString:hintText]) {
        [parentView addSubview:viewArrow];
    } else {
        
        left = SCREEN_WIDTH - 64 - 64 - 22;
    }
    
    UILabel *lblContent = [[UILabel alloc] initWithFrame:CGRectMake(left, 0, viewArrow.frame.origin.x - left - 15 + 15 + 16, 22 + 8)];
    [parentView addSubview:lblContent];
    lblContent.center = CGPointMake(lblContent.center.x, parentView.frame.size.height/2);
    lblContent.text = hintText;
    lblContent.backgroundColor =[UIColor clearColor];
    lblContent.textAlignment = NSTextAlignmentRight;
    lblContent.font = [UIFont fontWithName:@"Arial" size:16];
    
    if ([GlobalUnit isBlankString:hintText]) {
        
        lblContent.textColor = EastMarket_darkGreyFontColor;
    } else {
        
        lblContent.textColor = EastMatket_ConsumerHotLineColor;
    }
    return lblContent;
}
// 送达日期选择器
- (void)showPickerView:(BOOL)isDate selectList:(NSMutableArray *)selectList indexPath:(NSInteger)indexPath{
    NSString *typeStr = nil;
    if (isDate) {
        typeStr = @"送达日期";
    } else {
    
        typeStr = @"付款方式";
    }
    [ActionSheetStringPicker showPickerWithTitle:typeStr rows:@[(NSArray *)selectList] initialSelection:@[@""]  doneBlock:^(ActionSheetStringPicker *picker, NSArray *selectedIndex, NSArray *selectedValue) {
        //将原有的数据改变
        if (isDate) {
            strDeliveryDayGlobal = [NSString stringWithFormat:@"%@(%@)", [[selectedValue firstObject] objectForKey:@"name"], [[selectedValue firstObject] objectForKey:@"shortDate"]];
            debugLog(@"***************************%@", strDeliveryDayGlobal);
        } else {
        
            strPayTypeGloabal = [NSString stringWithFormat:@"%@", [[selectedValue firstObject] objectForKey:@"title"]];
            debugLog(@"***************************%@", strPayTypeGloabal);
        }
        [_tabView reloadData];
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:indexPath];
        [_tabView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        
    } cancelBlock:^(ActionSheetStringPicker *picker) {
    } origin:self.view];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
